package com.hy.study.driver;

import com.hy.study.mapper.NLineWordCountMapper;
import com.hy.study.reduce.NLineWordCountReducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.NLineInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author huyong
 * @date 2024 年 01 月 31 日 10:17: 52
 */
public class NLineWordCountDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        final String inputfile = "F:\\study\\Hadoop\\input\\nline_word_count";
        final String outputfile = "F:\\study\\Hadoop\\output\\Nline_result";
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setInputFormatClass(NLineInputFormat.class);

        //7设置每个切片InputSplit中划分三条记录
        NLineInputFormat.setNumLinesPerSplit(job,3);
        job.setMapperClass(NLineWordCountMapper.class);
        job.setReducerClass(NLineWordCountReducer.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setJarByClass(NLineWordCountReducer.class);

        FileInputFormat.addInputPath(job, new Path(inputfile));
        FileOutputFormat.setOutputPath(job, new Path(outputfile));

        boolean completion = job.waitForCompletion(true);
        System.exit(completion ? 0 : 1);
    }
}
